package com.elegantmall.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.elegantmall.pojo.GoodsPojo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import com.elegantmall.pojo.GoodsPojo;

import java.util.List;

@Mapper
@Repository
public interface GoodsDao extends BaseMapper<GoodsPojo> {
    // 使用原生MyBatis注解
    @Insert("INSERT INTO goods(goods_id, goods_name, price, number, seller_name, category, description, image, status) " +
            "VALUES(#{goodsId}, #{goodsName}, #{price}, #{number}, #{sellerName}, #{category}, #{description}, #{image}, #{status})")
    int insertGoods(GoodsPojo good);

    @Delete("DELETE FROM goods WHERE goods_id = #{goodsId} AND seller_name = #{sellerName}")
    int deleteBySeller(@Param("goodsId") String goodsId, @Param("sellerName") String sellerName);

    @Select("SELECT * FROM goods WHERE goods_id = #{goodsId}")
    GoodsPojo selectByGoodsId(String goodsId);

    @Select("SELECT * FROM goods WHERE seller_name = #{sellerName}")
    List<GoodsPojo> selectBySellerName(@Param("sellerName") String sellerName);

    @Update("<script>" +
            "UPDATE goods " +
            "<set>" +
            "<if test='goodsName != null'>goods_name = #{goodsName},</if>" +
            "<if test='price != null'>price = #{price},</if>" +
            "<if test='number != null'>number = #{number},</if>" +
            "<if test='category != null'>category = #{category},</if>" +
            "<if test='description != null'>description = #{description},</if>" +
            "<if test='image != null'>image = #{image},</if>" +
            "<if test='status != null'>status = #{status},</if>" +
            "</set>" +
            "WHERE goods_id = #{goodsId} AND seller_name = #{sellerName}" +
            "</script>")
    int updateGoodsInfo(GoodsPojo goodsPojo);
}
